package com.yntsoft.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yntsoft.entity.Permission;
import com.yntsoft.entity.RolePermission;
import com.yntsoft.mapper.PermissionMapper;
import com.yntsoft.mapper.RolePermissionMapper;
import com.yntsoft.util.MemcachedObjectType;

/**
 * 权限service
 * @author Tony
 *
 */
@Service
public class PermissionService extends BaseService{

	@Autowired
	private PermissionMapper permissionMapper;
	
	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	
	/**
	 * 通过用户id 获取用户权限
	 * @param id
	 * @return
	 * 
	 * String key = MemcachedObjectType.TASK.getPrefix() + id + this.getMemcachedVersion(MemcachedObjectType.TASK_VERSION.getPrefix()+id);
		String jsonString = memcachedClient.get(key);
		if (jsonString != null) {
			return jsonMapper.fromJson(jsonString, Task.class);
		} else {
			Task task = taskDao.selectByPrimaryKey(id);
			if (task != null) {
				jsonString = jsonMapper.toJson(task);
				memcachedClient.set(key, MemcachedObjectType.TASK.getExpiredTime(), jsonString);
			}
			return task;
		}
	 */
	public List<Permission> queryPermission(Integer userId,Integer type) {
		/*String  key = MemcachedObjectType.PERMISSION_PAGE.getPrefix()+userId+type+this.getMemcachedVersion(MemcachedObjectType.PERMISSION_PAGE_VERSION.getPrefix()+userId+type);
		String jsonString = memcachedClient.get(key);
		if (jsonString != null) {
			return jsonMapper.fromJson(jsonString, List.class);
		} else {
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("userId", userId);
			map.put("type", type);
			List<Permission> permissionList = permissionMapper.queryByUserId(map);
			if (permissionList != null) {
				jsonString = jsonMapper.toJson(permissionList);
				memcachedClient.set(key, MemcachedObjectType.TASK.getExpiredTime(), jsonString);
			}
			permissionList = setPermissionByParam(permissionList,map);
			return permissionList;
		}*/
		
		Map<String, Object> map = new HashMap<String,Object>();
        map.put("userId", userId);
        map.put("type", type);
        List<Permission> permissionList = permissionMapper.queryByUserId(map);
        /*if (permissionList != null) {
            jsonString = jsonMapper.toJson(permissionList);
            memcachedClient.set(key, MemcachedObjectType.TASK.getExpiredTime(), jsonString);
        }*/
        permissionList = setPermissionByParam(permissionList,map);
        return permissionList;
	}
	
	private List<Permission> setPermissionByParam(List<Permission> permissionList,Map<String, Object> map) {
		for(int i = 0 ; i< permissionList.size() ; i++){
			Permission permission = permissionList.get(i);
			map.put("parentid", permission.getId());
			List<Permission> children = permissionMapper.queryByUserId(map);
			if(children.size() != 0){
				children = setPermissionByParam(children,map);
				permission.setChildrenPermission(children);
			}else{
				permission.setChildrenPermission(children);
			}
		}
		return permissionList;
	}


	/**
	 * @param searchParam
	 * @param pagesize
	 * @return
	 */
	public PageInfo<Permission> queryPermissionList(Map<String, Object> searchParam, Integer pageSize) {
		Integer currentpage = 1;
		if(null != searchParam.get("currentpage") && !searchParam.get("currentpage").equals("")) {
			currentpage = Integer.parseInt(searchParam.get("currentpage").toString());
		}
		PageHelper.startPage(currentpage,pageSize);
		List<Permission> permissionList = permissionMapper.queryPermissionList(searchParam);
		//递归调用分级
		permissionList = setPermission(permissionList);
		PageInfo<Permission> pageInfo = new PageInfo<Permission>(permissionList);
		return pageInfo;
	}

	private List<Permission> setPermission(List<Permission> permissionList) {
		for(int i = 0 ; i< permissionList.size() ; i++){
			Permission permission = permissionList.get(i);
			List<Permission> children = permissionMapper.queryPermissionById(permission.getId());
			if(children.size() != 0){
				children = setPermission(children);
				permission.setChildrenPermission(children);
			}else{
				permission.setChildrenPermission(children);
			}
		}
		return permissionList;
	}

	public Integer savePermission(Permission permission) {
		if(permission.getId() != null){
			return permissionMapper.updateByPrimaryKey(permission);
		}else{
			return permissionMapper.insertSelective(permission);	
		}
		
	}

	public Permission getPermissionById(Integer PermissionById) {
		return permissionMapper.selectByPrimaryKey(PermissionById);
	}

	public Integer permissionDel(String ids) {
		String[] realId = ids.split(",");
		List<String> idList = new ArrayList<>();
		for(int i = 0 ;i < realId.length ;i++ ){
			idList.add(realId[i]);
		}
		return permissionMapper.permissionDel(idList);
	}

	/**
	 * 获取权限菜单列表
	 * @return
	 */
	public List<Permission> queryPermissionMenu(String type) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("type", Permission.TYPE_MENU);
		map.put("rank", type);
		return permissionMapper.queryPermissionMenu(map);
	}

	public List<Permission> queryMenu(Integer userId, Integer typeMenu,Integer parentId) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("type", typeMenu);
		map.put("parentId", parentId);
		List<Permission> permissionList = permissionMapper.queryFirstMenu(map);
		permissionList = setPermissionByParam(permissionList,map);
		return permissionList;
	}

	/**
	 * 获取角色的权限
	 * @param roleId
	 * @return
	 */
	public List<Permission> getRolePermission(String roleId) {
		List<Permission> rolePermissionList = permissionMapper.getRolePermission(roleId);
		return rolePermissionList;
	}
	
	public  List<Permission> getPermissionList() {
		List<Permission> permissionList = permissionMapper.getPermissionList();
		return permissionList;
	}

	/**
	 * 保存角色权限
	 * @return
	 */
	public Integer saveRolePermission(String permissionIds,String roleId) {
		Integer rst = rolePermissionMapper.deleteByRoleId(roleId);
		String[] permission = permissionIds.split(",");
		for(int i = 0 ; i < permission.length ; i++){
			RolePermission rolePermission = new RolePermission();
			rolePermission.setRoleId(Integer.valueOf(roleId));
			rolePermission.setPermissionId(Integer.valueOf(permission[i]));
			Integer addRst = rolePermissionMapper.insertSelective(rolePermission);
			if( addRst !=null &&  addRst != 0 ){
				
			}else{
				Integer.valueOf("a");
			}
		}
		return 1;
	}
}

